var app = getApp();
Page({
    data:{
        siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
        article : {},
        sortname : '',
        sortid : 0,
        content : '',
        gid : 0,
		pic : [],
    },
    onLoad:function(options){
        var self = this;
        var id = options.id ? options.id : '';

        app.com.http('quest','POST',{op:'jyinfo'},0,false,'','',function(res){
            if (!res.data.errno && res.data) {
                self.setData({
                    params : res.data.data.params,
                    sharedata : res.data.data.sharedata,
                    inited : true,
                });
            }else{
                app.com.alert(res.data.message,function(){
                    app.com.backPage();
                });
            }
        });
        wx.setNavigationBarTitle({
            title:  '建议',
        });
    },
    onShareAppMessage : function(){
        var self = this;
        return {
            title: '',
            path: '',
        }
    },
    onPullDownRefresh: function () {
        var self = this;
        app.com.confirm('是否要清空内容？',function(){
            self.setData({
                content : '',
                pic : [],
            })
        },false,'取消','清空');
        wx.stopPullDownRefresh();
    },
    contentfn(e){
        this.setData({
            content : e.detail.value
        })
    },
    pub(){
        var self = this;

        if(self.data.params.needauth == 1){
            self.loginauth = this.selectComponent('#loginauth');
            self.loginauth.hideauth();
            return false;
        }

        var postdata = {
            op : 'addjy',
            content : self.data.content,
            pic : self.data.pic ? JSON.stringify(self.data.pic) : '',
        };
        app.com.http('quest','POST',postdata,0,true,'','',function(res){
            if (!res.data.errno && res.data) {
                app.com.alert(res.data.message,function(){
                    app.com.backPage(0);
                });
            }else{
                app.com.alert(res.data.message);
            }
        });
        self.subMess();
    },
    authed(e){
        var self = this;
        self.setData({
            'params.needauth' : 0,
        })
        self.pub();
    },
    addimg(e){
        var self = this;
        var num = self.data.pic.length >= 12 ? 12 : 12-self.data.pic.length;
        wx.chooseImage({
            count: num, // 默认9
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success (res) {

                if( !self.data.pic || self.data.pic.length <= 0 ){
                    self.data.pic = [];
                }
                for(var t in res.tempFilePaths){
                    (function(m){

                        if( self.data.pic.length >= 12 ){
                            app.com.toast('最多12张图片','none');
                            return false;
                        }
                        self.uptomaster(res.tempFilePaths[m],function(resa){

                            if( self.data.pic.length >= 12 ){
                                app.com.toast('最多12张图片','none');
                                return false;
                            }
                            var temp = {id:m,att:resa.attachment,show:resa.url};
                            self.setData({
                                'pic' : self.data.pic.concat(temp)
                            });
                        });

                    })(t)
                }
            }
        })
    },
    onRemoveImgTap: function(e) {
        var index = e.currentTarget.dataset.index;
        this.data.pic.splice(index,1)
        this.setData({
            pic: this.data.pic
        });
    },
    uptomaster(tempurl,fn,type){
        wx.showLoading({mask:true,title:'上传中'});

        var url = app.siteInfo.siteroot+'?i='+app.siteInfo.uniacid+'&j=&c=utility&a=file&do=upload&type=image&thumb=0';
        if(type == 'vedio'){
            var url = app.com.murl('myinit',{op:'upvedio'});
        }  
        wx.uploadFile({
            url: url,
            filePath: tempurl,
            name: 'file',
            success: function(res){
                var aaa = JSON.parse( res.data );
                if(aaa.attachment){
                    if( fn ) fn( aaa );
                }else{
                    app.com.alert('上传失败'+aaa.res);
                }
            },
            complete : function(){
                wx.hideLoading();
            },
            fail(rs){
                console.log(rs)
            }
        })
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e);
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    showimages : function( e ){
        app.com.showimages( e );
    },
    weburl : function(e){
        app.com.toweburl( e,this );
    },
    footNavigate(e){
        var url = e.currentTarget.dataset.url;
        wx.switchTab({
          url: url
        })
    },
    subMess(){
        var self = this;
        wx.requestSubscribeMessage({
            tmplIds : self.data.params.submess,
            complete : function(){}
        })        
    }

})